package com.htudata.core.util.operation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MapOperationUtils {

  private HashMap<String, String> demoMap = new HashMap<String, String>() {
    
    };
  

  public static boolean isValidMap(Map map) {
    if (map != null && 
      !map.isEmpty())
      return true; 
    return false;
  }
  
  public static <K, V> List<K> getMapKeyForList(Map<K, V> map) {
    List<K> result = new ArrayList<>();
    for (Map.Entry<K, V> entry : map.entrySet())
      result.add(entry.getKey()); 
    return result;
  }
  
  public static <K, V> List<V> getMapValForList(Map<K, V> map) {
    List<V> result = new ArrayList<>();
    for (Map.Entry<K, V> entry : map.entrySet())
      result.add(entry.getValue()); 
    return result;
  }
  
  public static List<Map.Entry<String, String>> sortMap(Map<String, String> map, final String sortFieldName, final String sortType) {
    List<Map.Entry<String, String>> result = new ArrayList<>(map.entrySet());
    Collections.sort(result, new Comparator<Map.Entry<String, String>>() {
          public int compare(Map.Entry<String, String> entry1, Map.Entry<String, String> entry2) {
            int result = -1;
            if ("key".equals(sortFieldName)) {
              if ("asc".equals(sortType)) {
                result = ((String)entry1.getKey()).compareTo(entry2.getKey());
                return result;
              } 
              if ("desc".equals(sortType)) {
                result = ((String)entry2.getKey()).compareTo(entry1.getKey());
                return result;
              } 
            } 
            if ("val".equals(sortFieldName)) {
              if ("asc".equals(sortType)) {
                result = ((String)entry1.getValue()).compareTo(entry2.getValue());
                return result;
              } 
              if ("desc".equals(sortType)) {
                result = ((String)entry2.getValue()).compareTo(entry1.getValue());
                return result;
              } 
            } 
            return result;
          }
        });
    return result;
  }
}
